trigger Email on MZ_Playlist_Entry__c (before insert, before delete) {
    if (Trigger.isInsert) { 
        
        Messaging.reserveSingleEmailCapacity(trigger.size);
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        
        map<id ,id  > lengthMap = new map<id ,id>();
        for(MZ_Playlist_Entry__c entry:trigger.new){
            lengthMap.put(entry.MZ_Playlist_ref__c , entry.MZ_Track_ref__c );
        }
        map<id,MZ_Playlist__c> mapPlaylist = new map<id, MZ_Playlist__c>(
            [Select id,Name from MZ_Playlist__c where id=:lengthMap.keySet()]);
    
        map<id,MZ_Track__c > mapTrack = new map <id,MZ_Track__c > (
            [Select id,Name from MZ_Track__c where id=:lengthMap.values() ]);
        for (id palylistid:lengthMap.keySet() ) {
           
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(new String[] {'michal.zaborowski@britenet.com.pl'});
            email.setSubject('Add new track');
            email.setPlainTextBody( mapPlaylist.get(palylistid).Name + mapTrack.get(lengthMap.get(palylistid)).Name  );
            emails.add(email);
            
            
         }
         Messaging.sendEmail(emails);
        
    }
    if (Trigger.isDelete) { 
        
        Messaging.reserveSingleEmailCapacity(trigger.size);
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        
        map<id ,id  > lengthMap = new map<id ,id>();
        for(MZ_Playlist_Entry__c entry:trigger.old){
            lengthMap.put(entry.MZ_Playlist_ref__c , entry.MZ_Track_ref__c );
        }
        map<id,MZ_Playlist__c> mapPlaylist = new map<id, MZ_Playlist__c>(
            [Select id,Name from MZ_Playlist__c where id=:lengthMap.keySet()]);
    
        map<id,MZ_Track__c > mapTrack = new map <id,MZ_Track__c > (
            [Select id,Name from MZ_Track__c where id=:lengthMap.values() ]);
        for (id palylistid:lengthMap.keySet() ) {
           
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(new String[] {'michal.zaborowski@britenet.com.pl'});
            email.setSubject('Delete track');
            email.setPlainTextBody( mapPlaylist.get(palylistid).Name + mapTrack.get(lengthMap.get(palylistid)).Name );
            emails.add(email);
            
            
         }
         Messaging.sendEmail(emails);
        
    }
}